<?php

namespace Admin\Controller;

class TestController extends AdminbaseController {

    protected $vipModel;
    protected $shopModel;
    protected $payModel;
    protected $courseModel;
    protected $coachModel;
    protected $courseOrderModel;
    protected $courseRecordModel;
    protected $vipOrderModel;
    protected $vipShopRelationshipModel;
    protected $vipShopRelationshipLogModel;      
    protected $trainLogModel;
    protected $timeManagerModel;
    
    function _initialize() {
        parent::_initialize();
        $this->vipModel = D('vip');
        $this->shopModel = D('shop');
        $this->payModel = D('pay');
        $this->courseModel = D('course');
        $this->coachModel = D('coach');
        $this->courseOrderModel = D('courseOrder');
        $this->courseRecordModel = D('courseRecord');
        $this->vipOrderModel = D('vipOrder');
        $this->vipShopRelationshipModel = D('vipShopRelationship');
        $this->vipShopRelationshipLogModel = D('vipShopRelationshipLog');
        $this->trainLogModel = D('trainLog');
        $this->timeManagerModel = D('timeManager');
    }

    public function index() {
        
        $this->assign('channels', C('VIP_ORDER_CHANNEL'));
        $this->assign('cards', C('VIP_ORDER_BUY_TYPE'));
        $this->assign('paytype', C('VIP_ORDER_PAY_TYPE'));
        
        $this->display();
        
    }
    
    public function buyPost() {
        
        $mobile = I('post.mobile');
        $begin = I('post.begin');
        $end = I('post.end');
        $contractNo = I('post.contractNo');
        $shopId = 1;
        $buyType = I('post.cardType');
        $termNumber = I('post.termNumber');
        $payType = I('post.payType');
        $channel= I('post.channel');
        $price = I('post.price');
        $finger = I('post.finger');
        $cardNumber = I('post.cardNumber');
        
        $vipMap['mobile'] = $mobile;
        $vip = D('vip')->where($vipMap)->find();
        
        //增加线下订单
        $orderData['contract_no'] = $contractNo;
        $orderData['vip_id']=$vip['vip_id'];
        $orderData['shop_id']=$shopId;
        $orderData['create_time']=  getCurrentDate();
        $orderData['buy_type']=$buyType;
        $orderData['term_number']=$termNumber;
        $orderData['source']=C('VIP_ORDER_SOURCE.OFFLINE');
        $orderData['sale_id']=0;
        $orderData['channel']=$channel;
        $orderData['status'] = C('VIP_ORDER_STATUS.HAVING_PAY');
        $orderData['used_vip_id'] = $vip['vip_id'];
        $orderData['pay_type'] = $payType;
        $orderData['price'] = $price;
        
        $orderId = $this->vipOrderModel->add($orderData);
        $orderMap['order_id']=$orderId;
        $order = $this->vipOrderModel->where($orderMap)->find();
        
        //----------------------------
        //判断该店的指纹唯一性
        $fingerMap['shop_id'] = $shopId;
        $fingerMap['finger']=$finger;
        $fingerMap['vip_id']=array('neq',$order['vip_id']);
        $existFinger = $this->vipShopRelationshipModel->where($fingerMap)->find();
        if(!isNullOrEmpty($existFinger)){
            $this->error('该指纹已经被其他人录入了，请填写您自己的指纹！');
        }
        //判断该店卡号的唯一性
        $cardNumberMap['shop_id'] = $shopId;
        $cardNumberMap['card_number']=$cardNumber;
        $cardNumberMap['vip_id']=array('neq',$order['vip_id']);
        $existCardNumber = $this->vipShopRelationshipModel->where($cardNumberMap)->find();
        if(!isNullOrEmpty($existCardNumber)){
            $this->error('该卡号已经被其他人使用了！');
        }
        //查询会员与店是否关联
        $relationshipMap['shop_id'] = $shopId;
        $relationshipMap['vip_id'] = $order['vip_id'];
        $relationship = $this->vipShopRelationshipModel->where($relationshipMap)->find();
        
        //判断有无关系，有就续，没有就新建     
        if(isNullOrEmpty($relationship)) {
            //绑定会员与店关系
            $relationshipData['vip_id'] = $order['vip_id'];
            $relationshipData['shop_id'] = $shopId;
            $relationshipData['create_time'] = $begin;
            $relationshipData['status'] = C('VIP_SHOP_RELATIONSHIP_STATUS.VALID');
            $relationshipData['end_time'] = $end;
            $relationshipData['finger'] = $finger;
            $relationshipData['card_number'] = $cardNumber;
            
            //日志
            $relationshipLogData['begin_time']=$begin;
            $relationshipLogData['end_time']=$end;
            
            $relationshipId = $this->vipShopRelationshipModel->add($relationshipData);
            $relationship = $this->vipShopRelationshipModel->where(array('relationship_id'=>$relationshipId))->find();       
        } 
        //添加续约log
        $relationshipLogData['relationship_id'] = $relationship['relationship_id'];
        $relationshipLogData['buy_type'] = $order['buy_type'];
        $relationshipLogData['term_number'] = $order['term_number'];
        $relationshipLogData['order_id'] = $orderId;
        $relationshipLogData['create_user_id'] = $_SESSION["ADMIN_ID"];
        $relationshipLogData['create_time'] = getCurrentDate();
            
        $this->vipShopRelationshipLogModel->add($relationshipLogData);   
      
        //更新订单状态
        $order['status'] = C('VIP_ORDER_STATUS.USED');
        $order['used_time'] = $relationshipLogData['begin_time'];
        $this->vipOrderModel->where(array('order_id'=>$orderId))->save($order);
        
        $this->redirect('admin/test/index');
        
    }
    
    public function test() {
        
        vendor("PHPExcel.PHPExcel");
        vendor("PHPExcel.PHPExcel.IOFactory");

        $file_name='F:\new-fit24\test.xlsx';
        
        $fileParts = pathinfo($file_name);
        
        $extension = $fileParts['extension'];
        
        if ($extension == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        } else {
             $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        }

        $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
        
        for($i=2;$i<=$highestRow;$i++) {   
            
            if(!isNullOrEmpty($objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue())) {
            
                $vipMap['mobile'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();

                $vip = D('vip')->where($vipMap)->find();

                $vipId = $vip['vip_id'];

                if(isNullOrEmpty($vip)) {

                    $vipData['mobile'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
                    $vipData['vip_name'] = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                    $vipData['idcard_number'] = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                    $vipData['birthday'] = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                    $vipData['gender'] = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue() == '男' ? C('VIP_GENDER.MALE') : C('VIP_GENDER.FEMALE');
                    $vipData['token'] = createUuid();

                    $vipId = D('vip')->add($vipData);

                    $vip = D('vip')->where(array('vip_id'=>$vipId))->find();

                }

                $orderData['vip_id'] = $vipId;
                $orderData['create_time'] = getCurrentDate();

                $buyTypeName = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                $buyType = C('VIP_ORDER_BUY_TYPE.YEAR');

                if($buyTypeName == C('VIP_ORDER_BUY_TYPE.MONTH_STRING')) {
                    $buyType = C('VIP_ORDER_BUY_TYPE.MONTH');
                } else if($buyTypeName == C('VIP_ORDER_BUY_TYPE.SEASON_STRING')) {
                    $buyType = C('VIP_ORDER_BUY_TYPE.SEASON');
                } else if($buyTypeName == '半年卡') {
                    $buyType = 4;
                }

                $orderData['buy_type'] = $buyType;
                $orderData['term_number'] = $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();

                $payTypeName = $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
                $payType = C('VIP_ORDER_PAY_TYPE.OFFLINE_ALIPAY');

                if($payTypeName == '刷卡') {
                    $payType = C('VIP_ORDER_PAY_TYPE.POS');
                } else if($payTypeName == '现金') {
                    $payType = C('VIP_ORDER_PAY_TYPE.CASH');
                } else if($payTypeName == '支付宝') {
                    $payType = C('VIP_ORDER_PAY_TYPE.OFFLINE_ALIPAY');
                } 

                $orderData['pay_type'] = $payType;

                $code = $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();

                $saleMap['code'] = $code;

                $sale = D('sale')->where($saleMap)->find();

                $orderData['sale_id'] = $sale['sale_id'];
                $orderData['used_vip_id'] = $vipId;
                $orderData['source'] = C('VIP_ORDER_SOURCE.OFFLINE');
                $orderData['channel'] = C('VIP_ORDER_CHANNEL.PRESALE');
                $orderData['status'] = C('VIP_ORDER_STATUS.HAVING_PAY');
                $orderData['shop_id'] = 3;
                $orderData['price'] = $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();

                $order = D('vipOrder')->where($orderData)->find();
                
                if(isNullOrEmpty($order)) {
                    D('vipOrder')->add($orderData);
                }
                
                echo $orderData['term_number'].','.$highestRow;
            }
        } 

    }
    
}